import path from 'path';
import { argv } from 'yargs';
import webpack from 'webpack';

import ForkTsCheckerWebpackPlugin from 'fork-ts-checker-webpack-plugin';
// import { CleanWebpackPlugin } from 'clean-webpack-plugin';

import ProjectConfig from './project.constants';
import { getCssLoaders } from '../utils/webpack.util';

const IsDev = argv['dev'] as boolean;

const fixedPlugins = [
    new webpack.DefinePlugin({
        'process.env': {
            BROWSER: JSON.stringify(true),
            ROOT_STATIC_HOST: JSON.stringify(IsDev ? 'http://localhost:9627/' : ''),
        },
    }),
    // refer to https://github.com/TypeStrong/fork-ts-checker-webpack-plugin
    new ForkTsCheckerWebpackPlugin({
        typescript: {
            configFile: path.resolve(ProjectConfig.PROJECT_PATH, './tsconfig.json'),
        },
    }),
];

const swcBuildModuleOneOfRules: webpack.RuleSetRule[] = [
    {
        test: /\.(tsx|js|jsx)$/,
        loader: 'swc-loader',
        exclude: /.*node_modules/,
    },
];

const babelBuildModuleOneOfRules: webpack.RuleSetRule[] = [
    {
        test: /\.(tsx|js|jsx)$/,
        loader: 'babel-loader',
        options: { cacheDirectory: true },
        exclude: /.*node_modules/,
    },
];

function getCommonModuleOneOfRules(isDev: boolean): webpack.RuleSetRule[] {
    const commonOneOfRules: webpack.RuleSetRule[] = [
        {
            test: /\.s[ac]ss$/,
            use: [
                ...getCssLoaders(2, isDev),
                {
                    loader: 'sass-loader',
                    options: {
                        sourceMap: isDev,
                        // use dart-css
                        implementation: require('sass'),
                    },
                },
            ],
        },
        {
            test: /\.less$/,
            use: [
                ...getCssLoaders(2, isDev),
                {
                    loader: 'less-loader',
                    options: {
                        sourceMap: isDev,
                    },
                },
            ],
        },
        {
            test: /\.css$/,
            use: getCssLoaders(1, isDev),
        },
        {
            test: /\.(bmp|gif|jpg|jpeg|png|ico)$/,
            use: [
                {
                    loader: 'url-loader',
                    options: {
                        limit: 10 * 1024,
                        name: '[name].[contenthash:8].[ext]',
                        outputPath: 'assets/images',
                    },
                },
            ],
        },
        {
            test: /\.svg$/,
            use: ['@svgr/webpack', 'url-loader'],
        },
        {
            test: /\.(ttf|woff|woof2|eot|otf)$/,
            use: [
                {
                    loader: 'file-loader',
                    options: {
                        name: '[name].[contenthash:8].[ext]',
                        outputPath: 'assets/fonts',
                    },
                },
            ],
        },
        {
            test: /\.ts?$/,
            loader: 'ts-loader',
            //refer to https://github.com/TypeStrong/ts-loader
            options: {
                // disable type check, we will check it by using fork plugin
                transpileOnly: true,
            },
            exclude: /.*node_modules/,
        },
    ];

    return commonOneOfRules.concat(isDev ? swcBuildModuleOneOfRules : babelBuildModuleOneOfRules);
}

export default {
    fixedPlugins,
    getCommonModuleOneOfRules,
    swcBuildModuleOneOfRules,
    babelBuildModuleOneOfRules,
};
